package com.example.gfsquadwebviewproject.dialog.update;

import android.app.Dialog;
import android.content.Context;
import android.os.Handler;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.ProgressBar;
import android.widget.TextView;
import com.example.gfsquadwebviewproject.R;
import com.example.gfsquadwebviewproject.util.MyCountDownTimer;

public class UpdateDialog extends Dialog implements MyCountDownTimer.ICountDownCallback{
    Context context;

    ProgressBar progressBar;
    TextView tv_load_progress;

    Button btn_sure;

    //限时倒数
    TextView tv_timeTick;
    MyCountDownTimer myCountDownTimer;////限时控件

    Handler handler;

    public UpdateDialog(Context context) {
        super(context, R.style.dialog2);
        this.context=context;
        handler = new Handler();

        View mView = LayoutInflater.from(context).inflate(R.layout.dialog_update_progress, null);

        tv_timeTick = mView.findViewById(R.id.tv_timeTick);
        tv_timeTick.setVisibility(View.INVISIBLE);

        btn_sure = mView.findViewById(R.id.btn_sure);
        tv_load_progress = mView.findViewById(R.id.tv_load_progress);
        progressBar = mView.findViewById(R.id.progressBar);

        btn_sure.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                dismiss();
            }
        });

        super.setContentView(mView);
    }

    public void setMsg(final String msg){
        if (handler!=null) {
            handler.post(new Runnable() {
                @Override
                public void run() {
                    tv_load_progress.setText(msg);
                }
            });
        }
    }

    public void setMaxProgress(final int maxProgress){
        if (handler!=null) {
            handler.post(new Runnable() {
                @Override
                public void run() {
                    progressBar.setMax(maxProgress);
                }
            });
        }
    }

    public void setProgress(final int progress){
        if (handler!=null) {
            handler.post(new Runnable() {
                @Override
                public void run() {
                    progressBar.setProgress(progress);
                    setMsg("进度： " + progress + "%");
                }
            });
        }
    }

    public void setAccomplish(){
        if (handler!=null) {
            handler.post(new Runnable() {
                @Override
                public void run() {
                    //显示时启动限时
                    if (myCountDownTimer == null) {
                        myCountDownTimer =
                                new MyCountDownTimer(context, UpdateDialog.this, 5);
                    }
                    myCountDownTimer.start();
                    tv_timeTick.setVisibility(View.VISIBLE);
                }
            });
        }
    }

    @Override
    protected void onStop() {
        super.onStop();
        //停止限时
        if (myCountDownTimer!=null) {
            myCountDownTimer.stop();
            myCountDownTimer = null;
        }
        handler = null;
    }

    //限时倒计时
    @Override
    public void onTimeTick(final int second) {
        if (handler!=null) {
            handler.post(new Runnable() {
                @Override
                public void run() {
                    tv_timeTick.setText("" + second);
                }
            });
        }
    }
    //限时 时间结束
    @Override
    public void onTimeOut() {
        dismiss();
    }


}
